package com.fh.shop.api.exception;

import com.fh.shop.api.common.ResponseEnum;
import com.fh.shop.api.common.ServerResponse;
import com.fh.shop.api.interceptor.IdempotenceInterceptor;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;

// 统一异常处理器
//@ControllerAdvice
//@ResponseBody
@RestControllerAdvice  // @RestControllerAdvice = @ControllerAdvice + @ResponseBody
public class WebExceptionHandler {

    @ExceptionHandler(TokenException.class) // 处理指定的异常
    public ServerResponse handleTokenException(TokenException tokenException) {
        ResponseEnum responseEnum = tokenException.getResponseEnum();
        return ServerResponse.error(responseEnum);
    }

    @ExceptionHandler(StockException.class)
    public ServerResponse handleStockException(StockException stockException) {
        return stockException.getResponse();
    }

    @ExceptionHandler(IdempotenceException.class)
    public ServerResponse handleIdempotenceException(IdempotenceException idempotenceException) {
        ResponseEnum responseEnum = idempotenceException.getResponseEnum();
        return ServerResponse.error(responseEnum);
    }

    @ExceptionHandler(PayException.class)
    public String handleIdempotenceException(PayException payException) {
        ResponseEnum responseEnum = payException.getResponseEnum();
        return "<h1 style='color:red;'>错误编码:"+responseEnum.getCode()+";提示信息:"+responseEnum.getMsg()+"</h1>";
    }

}
